import {defineStore} from "pinia";
import {UserLogin} from "../types/tanTypes/userLogin";
import {login} from "../api/login/login";
import {router} from "../router";


// 用户登录状态机
export const useUserLogin = defineStore('login', {
    state: () => ({
        // 用户信息数组
        userLoginObj: null
    }),
    actions: {
        // 用户登录异步函数Actions
        async userLoginActions(userMsgObj: UserLogin) {
            const userRes = await login(userMsgObj)
            // 保存用户信息
            this.userLoginObj = userRes.data
            await router.push('/pipe/pipeOverview')
        }
    },
    // 用户本地存储
    persist: {
        enabled: true,
        strategies: [{
                key: 'pinia-pressure-userInfo',
                storage: localStorage,
                paths: ['userLoginObj']
            }]
    },

})
